package com.gz.mylife.app;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.adapter.BaseAdapterHelper;
import com.gz.mylife.adapter.QuickAdapter;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.OfflineDialog;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.listener.DeleteHouseListener;
import com.zl.smartmall.library.listener.GetMyCommunityAddressListener;
import com.zl.smartmall.library.listener.SelectDefaultCommunityListener;
import com.zl.smartmall.library.po.GZCommunityInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.PreferenceModel;
import com.zl.smartmall.library.vo.GZCommunityInfoDAO;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MenuCompat;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MyCommunityActivity extends BaseActivity implements OnClickListener,OnLoadingCoverRefreshListener,OnRefreshListener<ListView>,OnItemClickListener{
	private static final int MSG_LOADDATA_FROM_DB = 1;

	public static final String MYCOMMUNITYACIVITY_REFRESH_TIME = "mycommunityactivity_refresh_time";
	
	private PullToRefreshListView mListView;
	private LoadingCover mLoadingCover;
	private OfflineDialog mOfflineDialog;
	private Dialog mLoadingData;
	
	private List<GZCommunityInfo> mDataLists;
	private MyAdapter mAdapter;
	
	private MyCommunityListener mListener;
	private MySelectDefaultCommunityListener mSelectDefaultCommunityListener;
	private MyDeleteHouseListener mDeleteHouseListener;
	
	private MyHandler mHandler;
	private int oldType;
	private int newType;
	private int pidType = -1;
	private int tempType = -1;
	private int page;
	private boolean unBind;
	private long mBackKeyPressedTime;
	private Dialog mMenudialog;
	private TextView mTvTitle;
	
	private GZCommunityInfo oldInfo;
	private GZCommunityInfo newInfo;
	
	private int mWindowWidth;
	private View mCurrentView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mycommunity);
		mDataLists = new ArrayList<GZCommunityInfo>();
		mListener = new MyCommunityListener(this);
		mSelectDefaultCommunityListener = new MySelectDefaultCommunityListener(this);
		mDeleteHouseListener = new MyDeleteHouseListener(this);
//		mActionBar.setTitle("我的小区");
		setActionbarTitle("我的小区", false);
		mHandler = new MyHandler(this);
		oldType = IGZAddress.getInstance().getCurrentCommunityID();
		page = getIntent().getIntExtra("page", 0);
		unBind = getIntent().getBooleanExtra("unbind", false);
		if(unBind){
			mActionBar.setDisplayHomeAsUpEnabled(false);
		}
		newType = oldType;
		WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
		mWindowWidth = wm.getDefaultDisplay().getWidth();
		initView();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mListener.release();
		mHandler.release();
		mDeleteHouseListener.release();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			return back();
		case 100:
			Intent intent = new Intent(getApplicationContext(), Plaza.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.putExtra("page_index", page);
			intent.putExtra("unBind", true);
			startActivity(intent);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	private void initView() {
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		
		mLoadingData = new Dialog(this, R.style.DialogTheme);
		mLoadingData.setContentView(R.layout.loading_data_dialog2);
		mLoadingData.setCancelable(false);
		mLoadingData.setCanceledOnTouchOutside(false);
		
		mOfflineDialog = (OfflineDialog) LayoutInflater.from(getApplicationContext()).inflate(R.layout.offline_dialog, null);
		mListView = (PullToRefreshListView) findViewById(R.id.lv_community);
		mAdapter = new MyAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnRefreshListener(this);
		mListView.setOnItemClickListener(this);
		
		findViewById(R.id.btn_add).setOnClickListener(this);
		loadDataFromDB();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem ensureItem = menu.add(0, 100, 0, "先逛逛");
		MenuCompat.setShowAsAction(ensureItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
		if(!unBind){
			ensureItem.setVisible(false);
		}
		return super.onCreateOptionsMenu(menu);
	}
	

	private QuickAdapter<GZCommunityInfo> buildAdapter() {
		return new QuickAdapter<GZCommunityInfo>(getApplicationContext(),R.layout.mycommunity_item,mDataLists) {

			@Override
			protected void convert(final BaseAdapterHelper helper,
					final GZCommunityInfo item) {
				helper.setText(R.id.tv_community, item.getCommunityName())
					.setText(R.id.tv_house, item.getHouseName());
				TextView selectTv = helper.getView(R.id.tv_default);
				if(item.getStatus() == 1){
					selectTv.setClickable(false);
					selectTv.setTextColor(getResources().getColor(R.color.red));
					selectTv.setText(getResources().getString(R.string.mycommunity_default_community));
				}else{
					selectTv.setClickable(true);
					selectTv.setTextColor(getResources().getColor(R.color.RGB_2F9BE6));
					selectTv.setText(getResources().getString(R.string.mycommunity_set_default_community));
					selectTv.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							tempType = helper.getPosition();
							mLoadingData.show();			
							Log.i("duzhijuan",item.getCmid()+"==================");
							IGZAddress.getInstance().selectDefaultCommunity(item.getCmid(), item.getPid(), mSelectDefaultCommunityListener);
						}
					});
				}
			}
		};
	}
	
	private static class MyCommunityListener extends GetMyCommunityAddressListener{
		private MyCommunityActivity mOuter;
		public MyCommunityListener(MyCommunityActivity outer){
			this.mOuter = outer;
		}
		
		public void release(){
			new WeakReference<MyCommunityActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess(List<GZCommunityInfo> lists) {
			if(mOuter == null){
				return;
			}
			mOuter.newType = IGZAddress.getInstance().getCurrentCommunityID();
			switch (requestCode) {
			case FIRST:
				mOuter.mDataLists.clear();
				mOuter.mDataLists.addAll(lists);
				mOuter.mAdapter.notifyDataSetChanged();
				mOuter.mLoadingCover.onRefreshSuccess();
				for(int i = 0;i<lists.size();i++){
					if(lists.get(i).getStatus()==1){
						mOuter.pidType = i;
					}
				}
				mOuter.saveToDB(lists);
				break;
			case PULL:
				mOuter.mDataLists.clear();
				mOuter.mDataLists.addAll(lists);
				mOuter.mAdapter.notifyDataSetChanged();
				mOuter.mListView.getLoadingLayoutProxy().refreshSuccess();
				mOuter.saveToDB(lists);
				for(int i = 0;i<lists.size();i++){
					if(lists.get(i).getStatus()==1){
						mOuter.pidType = i;
					}
				}
				break;

			default:
				break;
			}
			
		}

		@Override
		public void onNoMoreData() {
			if(mOuter == null){
				return;
			}
			switch (requestCode) {
			case FIRST:
				mOuter.mLoadingCover.onRefreshSuccess();
				break;
			case PULL:
				mOuter.mListView.getLoadingLayoutProxy().refreshSuccess();
				break;

			default:
				break;
			}
			
		}

		@Override
		public void onNoNetWork() {
			if(mOuter == null){
				return;
			}
			
			switch (requestCode) {
			case FIRST:
				mOuter.mLoadingCover.onRefreshSuccess();
				Toast.makeText(mOuter, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
				break;
			case PULL:
				mOuter.mListView.getLoadingLayoutProxy().refreshFailed();
				Toast.makeText(mOuter, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
			
		}

		@Override
		public void onOffline() {
			if(mOuter == null){
				return;
			}
			
			switch (requestCode) {
			case FIRST:
				mOuter.mLoadingCover.onRefreshSuccess();
				mOuter.mOfflineDialog.show();
				break;
			case PULL:
				mOuter.mListView.getLoadingLayoutProxy().refreshFailed();
				mOuter.mOfflineDialog.show();
				break;

			default:
				break;
			}
			
		}

		@Override
		public void onResponseFailed() {
			if(mOuter == null){
				return;
			}
			
			switch (requestCode) {
			case FIRST:
				mOuter.mLoadingCover.onRefreshSuccess();
				Toast.makeText(mOuter, "请求失败", Toast.LENGTH_SHORT).show();
				break;
			case PULL:
				mOuter.mListView.getLoadingLayoutProxy().refreshFailed();
				break;

			default:
				break;
			}
		}
		
	}

	@Override
	public void onLoadingCoverRefresh() {
		loadData();
	}

	private void loadData() {
		IGZAddress.getInstance().getMyCommunity(mListener);
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		refresh();
	}

	private void refresh() {
		IGZAddress.getInstance().refreshMyCommunity(mListener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_add:
			Intent intent = new Intent(getApplicationContext(), SelectCommunityAddressActivity.class);//进入选择省市县的界面
			intent.putExtra("titlename", "添加我的小区");
			startActivityForResult(intent, 1);
			break;

		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int resultcode, Intent arg2) {
		if(resultcode == 201){
			refresh();
		}
	}
	
	private void saveToDB(final List<GZCommunityInfo> lists){
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				PreferenceModel.putLong(MYCOMMUNITYACIVITY_REFRESH_TIME, System.currentTimeMillis());
				GZCommunityInfoDAO.DeleteAll(getApplicationContext());
				GZCommunityInfoDAO.saveAll(getApplicationContext(), lists);
			}
		});
	}
	
	private void loadDataFromDB(){
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				List<GZCommunityInfo> lists = GZCommunityInfoDAO.getAll(getApplicationContext());
				mHandler.obtainMessage(MSG_LOADDATA_FROM_DB, lists).sendToTarget();
			}
		});
	}
	
	private static class MyHandler extends Handler{
		private MyCommunityActivity mOuter;
		
		public MyHandler(MyCommunityActivity outer){
			this.mOuter = outer;
		}
		
		public void release(){
			new WeakReference<MyCommunityActivity>(mOuter);
		}
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOADDATA_FROM_DB:
				List<GZCommunityInfo> lists = (List<GZCommunityInfo>) msg.obj;
				if(lists != null && lists.size()>0){
					mOuter.mDataLists.clear();
					mOuter.mDataLists.addAll(lists);
					mOuter.mAdapter.notifyDataSetChanged();
					mOuter.mLoadingCover.onRefreshSuccess();
					
					for(int i = 0;i<lists.size();i++){
						if(lists.get(i).getStatus()==1){
							mOuter.pidType = i;
						}
					}
					if(System.currentTimeMillis() - PreferenceModel.getLong(MYCOMMUNITYACIVITY_REFRESH_TIME, 0)>1000*60*60*24){
						mOuter.refresh();
					}
				}else{
					mOuter.loadData();
				}
				break;

			default:
				break;
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			return back();

		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private static class MySelectDefaultCommunityListener extends SelectDefaultCommunityListener{
		private MyCommunityActivity mOuter;
		
		public MySelectDefaultCommunityListener(MyCommunityActivity outer){
			this.mOuter = outer;
		}
		
		public void release(){
			new WeakReference<MyCommunityActivity>(mOuter);
		}
		
		@Override
		public void onResponseSuccess() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
			if(mOuter.tempType != -1){
				mOuter.oldInfo = null;
				mOuter.newInfo = null;
				if(mOuter.pidType>=0){
					mOuter.oldInfo = mOuter.mDataLists.get(mOuter.pidType);
					mOuter.oldInfo.setStatus(0);
				}
				mOuter.newInfo = mOuter.mDataLists.get(mOuter.tempType);
				mOuter.newInfo.setStatus(1);
				mOuter.newType = mOuter.newInfo.getCmid();
				mOuter.pidType = mOuter.tempType;
				mOuter.tempType = -1;
				ThreadExecutor.getInstance().execute(new Runnable() {
					
					@Override
					public void run() {
						if(mOuter.oldInfo != null){
							GZCommunityInfoDAO.updata(mOuter.getApplicationContext(), mOuter.oldInfo);
						}
						if(mOuter.newInfo != null){
							GZCommunityInfoDAO.updata(mOuter.getApplicationContext(), mOuter.newInfo);
						}
					}
				});
				//IGZAddress.getInstance().setIDAndName(mOuter.newInfo.getCmid(), mOuter.newInfo.getCommunityName());
				IGZAddress.getInstance().setIDAndName(mOuter.newInfo.getCmid(), mOuter.newInfo.getHouseName().substring(0, mOuter.newInfo.getHouseName().indexOf(",")));
				//发送广播（改变商城首页顶部显示当前绑定小区的名称）
				Intent intent = new Intent("CHANCE_PLAZA_TITLE");
				mOuter.sendBroadcast(intent);
//				mOuter.mAdapter.upData(type);
//				mOuter.mAdapter.upData(mOuter.newType);
				mOuter.mAdapter.notifyDataSetChanged();
			}
			mOuter.mLoadingData.dismiss();
		}

		@Override
		public void onNoNetWork() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onOffline() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			mOuter.mOfflineDialog.show();
		}

		@Override
		public void onResponseFailed() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
			mOuter.mLoadingData.dismiss();
			Toast.makeText(mOuter.getApplicationContext(), "设置失败", Toast.LENGTH_SHORT).show();
		}
		
	}

	private boolean back() {
		if(unBind){
			if(System.currentTimeMillis()-mBackKeyPressedTime<1000*5){
				Intent intent = new Intent(Constants.ACTION_EXIT_WHOLE_APP);
				sendBroadcast(intent);
				finish();
			}else{
				mBackKeyPressedTime = System.currentTimeMillis();
				Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
			}
			return true;
		}
		if(oldType != newType){
			Intent intent = new Intent(Constants.ACTION_CHANGE_COMMUNITY);
			sendBroadcast(intent);
		}
		Intent intent = new Intent(getApplicationContext(), Plaza.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("page_index", page);
		startActivity(intent);
		return true;
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataLists.size();
		}

		@Override
		public Object getItem(int position) {
			return mDataLists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder mHolder;
			if(convertView == null){
				mHolder = new ViewHolder();
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.mycommunity_item, null);
				mHolder.mCommunity = (TextView) convertView.findViewById(R.id.tv_community);
				mHolder.mFloor = (TextView) convertView.findViewById(R.id.tv_house);
				mHolder.mSelectTv = (TextView) convertView.findViewById(R.id.tv_default);
				mHolder.mhs = (HorizontalScrollView) convertView.findViewById(R.id.hs_mycommunity_item);
				mHolder.mLlLeft = (LinearLayout) convertView.findViewById(R.id.ll_left);
				mHolder.mRlRight = (RelativeLayout) convertView.findViewById(R.id.rl_right);
				convertView.setTag(mHolder);
			}else{
				mHolder = (ViewHolder) convertView.getTag();
			}
			final GZCommunityInfo info = mDataLists.get(position);
			mHolder.mCommunity.setText(info.getHouseName().substring(0, info.getHouseName().indexOf(",")));
			mHolder.mFloor.setText(info.getHouseName().substring(info.getHouseName().indexOf(",") + 1));
			
			RelativeLayout.LayoutParams params = (LayoutParams) mHolder.mLlLeft.getLayoutParams();
			params.width = mWindowWidth;
			
			mHolder.mSelectTv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					tempType = position;
					mLoadingData.show();
					IGZAddress.getInstance().selectDefaultCommunity(info.getCmid(), info.getPid(), mSelectDefaultCommunityListener);
				}
			});
			if(info.getStatus() == 1){
				mHolder.mSelectTv.setClickable(false);
				mHolder.mSelectTv.setVisibility(View.VISIBLE);
				mHolder.mSelectTv.setTextColor(getResources().getColor(R.color.red));
				mHolder.mSelectTv.setText(getResources().getString(R.string.mycommunity_default_community));
			}else{
				mHolder.mSelectTv.setClickable(true);
				mHolder.mSelectTv.setVisibility(View.GONE);
				mHolder.mSelectTv.setTextColor(getResources().getColor(R.color.RGB_2F9BE6));
				mHolder.mSelectTv.setText(getResources().getString(R.string.mycommunity_set_default_community));
			}
			
			if(mHolder.mhs.getScrollX() != 0){
				mHolder.mhs.scrollTo(0, 0);
			}
			mHolder.mhs.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						ViewHolder holder3 = (ViewHolder) mCurrentView.getTag();
						if(holder3.mhs.getScrollX()>0&&holder3.mhs != v){
							holder3.mhs.scrollTo(0, 0);
						}else{
						}
						break;
					case MotionEvent.ACTION_UP:
						ViewHolder holder2 = (ViewHolder) v.getTag();
						int scrollx = holder2.mhs.getScrollX();
						int width = holder2.mRlRight.getWidth();
						if(scrollx<width/2){
							holder2.mhs.scrollTo(0, 0);
						}else{
							holder2.mhs.scrollTo(width, 0);
							mCurrentView = v;
						}
						break;

					default:
						break;
					}
					return false;
				}
			});
			
			mHolder.mLlLeft.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						if(mCurrentView != null){
							ViewHolder holder2 = (ViewHolder) mCurrentView.getTag();
							if(holder2.mhs.getScrollX()>0&&holder2.mLlLeft != v){
								holder2.mhs.scrollTo(0, 0);
							}else{
							}
							if(holder2.mLlLeft == v && holder2.mhs.getScrollX() > 0){
								return true;
							}
						}
						break;
					case MotionEvent.ACTION_UP:
						break;

					default:
						break;
					}
					return false;
				}
			});
			mHolder.mLlLeft.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					tempType = position;
					createDialog(info);
				}
			});
			mHolder.mRlRight.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					tempType = position;
					if(info.getStatus() == 1){
						Toast.makeText(getApplicationContext(), "您不能删除当前绑定的小区", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					/*mLoadingData.show();
					if(info.getCmid() == newType){//
						newType = -1;
						IGZAddress.getInstance().deleteSelected();
					}*/
					mDataLists.remove(info);
					mAdapter.notifyDataSetChanged();
					ThreadExecutor.getInstance().execute(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							GZCommunityInfoDAO dao = new GZCommunityInfoDAO();
							GZCommunityInfoDAO.delete(getApplicationContext(), info);
						}
					});
					
					IGZAddress.getInstance().deleteHouse(info.getCmid(), info.getPid(), mDeleteHouseListener);
				}
			});
			
			return convertView;
		}
		
		public void upData(int postion){
			int first = mListView.getRefreshableView().getFirstVisiblePosition();
			int last = mListView.getRefreshableView().getLastVisiblePosition();
			if(postion - first>=0&&last - postion >=0){
				View convertView = mListView.getChildAt(postion-first);
				ViewHolder mHolder = (ViewHolder) convertView.getTag();
				GZCommunityInfo info = mDataLists.get(postion);
				if(info.getStatus() == 1){
					mHolder.mSelectTv.setClickable(false);
					mHolder.mSelectTv.setTextColor(getResources().getColor(R.color.red));
					mHolder.mSelectTv.setText(getResources().getString(R.string.mycommunity_default_community));
				}else{
					mHolder.mSelectTv.setClickable(true);
					mHolder.mSelectTv.setTextColor(getResources().getColor(R.color.RGB_2F9BE6));
					mHolder.mSelectTv.setText(getResources().getString(R.string.mycommunity_set_default_community));
				}
			}
		}
		
		private class ViewHolder{
			public TextView mCommunity;
			public TextView mFloor;
			public TextView mSelectTv;
			public HorizontalScrollView mhs;
			public LinearLayout mLlLeft;
			public RelativeLayout mRlRight;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, final int position,
			long id) {
		// TODO Auto-generated method stub
//		AlertDialog.Builder builder = new Builder(this);
//		builder.setTitle("切换当前小区");
//		builder.setMessage("是否要切换当前小区？");
//		builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
//			
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				dialog.dismiss();
//			}
//		});
//		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
//			
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				mLoadingData.show();
//				tempType = position-1;
//				final GZCommunityInfo info = (GZCommunityInfo) mAdapter.getItem(tempType);
//				IGZAddress.getInstance().selectDefaultCommunity(info.getCmid(), info.getPid(), mSelectDefaultCommunityListener);
//				dialog.dismiss();
//			}
//		});
//		Dialog dialog = builder.create();
//		dialog.show();
		
		tempType = position-1;
		final GZCommunityInfo info = (GZCommunityInfo) mAdapter.getItem(tempType);
		
		createDialog(info);
	}

	private void createDialog(final GZCommunityInfo info) {
		mMenudialog = new Dialog(this, R.style.DialogTheme);
		View dialogView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.mycommunity_dialog_layout, null);
		mTvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
		mTvTitle.setText(info.getCommunityName());
		dialogView.findViewById(R.id.btn_change).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mLoadingData.show();
				IGZAddress.getInstance().selectDefaultCommunity(info.getCmid(), info.getPid(), mSelectDefaultCommunityListener);
				mMenudialog.dismiss();
			}
		});
		dialogView.findViewById(R.id.btn_delete).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenudialog.dismiss();
				if(info.getCmid() == newType){
					Toast.makeText(getApplicationContext(), "您不能删除当前绑定的小区", Toast.LENGTH_SHORT)
							.show();
					return;
				}
				mLoadingData.show();
				IGZAddress.getInstance().deleteHouse(info.getCmid(), info.getPid(), mDeleteHouseListener);
			}
		});
		dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenudialog.dismiss();
			}
		});
		mMenudialog.setContentView(dialogView);
		mMenudialog.setCancelable(false);
		mMenudialog.setCanceledOnTouchOutside(false);
		mMenudialog.show();
	}
	
	private static class MyDeleteHouseListener extends DeleteHouseListener{
		private MyCommunityActivity mOuter;
		
		public MyDeleteHouseListener(MyCommunityActivity outer){
			this.mOuter = outer;
		}
		
		public void release(){
			new WeakReference<MyCommunityActivity>(mOuter);
		}

		@Override
		public void onResponseSuccess() {
			if(mOuter == null){
				return;
			}
			
			//删除内存和db，同时处理删除默认小区的情况
//			final GZCommunityInfo info = mOuter.mDataLists.get(mOuter.tempType);
//			if(info.getCmid() == mOuter.newType){//
//				mOuter.newType = -1;
//				IGZAddress.getInstance().deleteSelected();
//			}
//			mOuter.mDataLists.remove(info);
//			mOuter.mAdapter.notifyDataSetChanged();
//			ThreadExecutor.getInstance().execute(new Runnable() {
//				
//				@Override
//				public void run() {
//					// TODO Auto-generated method stub
//					GZCommunityInfoDAO dao = new GZCommunityInfoDAO();
//					dao.delete(mOuter.getApplicationContext(), info);
//				}
//			});
//			mOuter.mLoadingData.dismiss();
		}

		@Override
		public void onNoNetWork() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
//			mOuter.mLoadingData.dismiss();
//			Toast.makeText(mOuter.getApplicationContext(), Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onOffline() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
//			mOuter.mLoadingData.dismiss();
//			mOuter.mOfflineDialog.dismiss();
		}

		@Override
		public void onResponseFailed() {
			// TODO Auto-generated method stub
			if(mOuter == null){
				return;
			}
//			mOuter.mLoadingData.dismiss();
//			Toast.makeText(mOuter.getApplicationContext(), "删除失败", Toast.LENGTH_SHORT).show();
		}
		
	}
}
